<template>
  <div class='share-btn-wrap' @click='shareBtnHandle' :style='{"top":top,"right":right,"z-index":zIndex}'>
    <div class="share-btn-image"></div>
  </div>
</template>

<script>
import fun from "@/util/function";
import errorPageVue from '../../pages/errorPage.vue';
// const URL_LINK = 'www.baidu.com'
const IMG_URL = 'https://saas-zyzq.oss-cn-shenzhen.aliyuncs.com/zyzqshare.jpg' // 分享图片
  export default {
    name: 'ShareBtn',
    props:{
      // 上下定位
      top:{
        type: String,
        default: '70vh'
      },
      // 左右定位
      right:{
        type: String,
        default: '10vw'
      },
      zIndex:{// 分享图标层次，home页和直播页层次不同
        type: String,
        default: '100'
      },
      type:{
        type: Number,
        default: 0 // 0是首页；1是直播，预告；2是回看
      },
      roomId:{   // 直播间id
        type: String, 
        default: '0'
      },
      lookBackId:{   // 回看id
        type: String,
        default: '0'
      }
    },
    data(){
      return {
        urlLink: '',
        title: '',
        message: '',
        imgUrl: '',
        info:'',  
        shareImgUrl :IMG_URL // 分享链接上的默认图片地址 // this.$store.state.SysOrgInfo.icon
      }
    },
    methods:{  
      //分享渠道 0:微信  1:微信朋友圈  2:QQ好友 3:QQ空间 4:腾讯微博 5:新浪微博 99:客户端选择
      sharefunction(url, title, message, img_url, sharetype){
        console.log('img_url', img_url)
        if(!url){
            //替换域名
            url = location.href.replace(location.protocol+"//"+location.host,cm.conf.serverHost);
            //替换路径
            url = url.replace(conf.tztPath,"/app");
        }
        if(!title){
            title = document.title;
        }
        if(!message){
            message = document.title;
        }
                
        var href = "http://action:10055?url=" + encodeURIComponent(url) + "&&title=" + encodeURIComponent(title) + "&&message=" +
            encodeURIComponent(message)+ (img_url ? ("&&IMG_URL=" + encodeURIComponent(img_url)) : "") + (sharetype ? ("&&" + encodeURIComponent(sharetype)) : "");      

        this.tztChangeURL(href);
      },
      tztChangeURL(str){
        var appversion = navigator.appVersion.toLocaleLowerCase();
        if(appversion.indexOf("windows phone")>0){
            window.external.notify(str);
        }else if(appversion.indexOf("iphone")>0){
            window.location.href=str;
        }else if(appversion.indexOf("android")>0){
            window.MyWebView.onJsOverrideUrlLoading(str);
        }else{
            window.location.href = str;
        }
      },
      // 获取首页的分享配置信息
      getHomeInfo(){
        return new Promise((resolve, reject)=>{
          this.$http.get("/h5live/getOrgShare").then(res => {            
            if(res.data.code === "000000") {              
              let shareInfoData = res.data.data   // 可能为null
              this.title = (shareInfoData && shareInfoData.shareTitle) || '【' + this.$store.state.SysOrgInfo.name + '直播】让思想的碰撞改变投资，专业，好看！' // 分享标题
              this.message = (shareInfoData && shareInfoData.shareDesc) || "现在下载" + this.$store.state.SysOrgInfo.name + "APP，速领银豆惊喜大礼。" //分享描述
              this.imgUrl = (shareInfoData && shareInfoData.shareIcon) || this.shareImgUrl
              let shareUrl ="zyzqHome/" + "?fromUn=" + this.$store.state.userInfo.un;
              this.urlLink = fun.getShareUrl(this.$channel,shareUrl);
              this.sharefunction(this.urlLink, this.title, this.message, this.imgUrl)
              // window.MyWebView.tzt_hsCallNativeFunctionFromWeb("reqaction", "{\"action\":\"10055\",\"url\":\"" + this.urlLink + "\",\"title\":\"" + this.title + "\",\"message\":\"" + this.message + "\",\"IMG_URL\":\""+ this.imgUrl +"\"}") // 方案三              
            }
          }).catch(err => {
            console.log(err)
            reject(err)
          });
        })
      },
      // 获取直播的分享配置信息
      getLiveInfo(){        
        return new Promise((resolve, reject)=>{
          this.$http.get("/h5live/getRoomSetting?roomId=" + this.roomId).then(res => {
            if(res.data.code === "000000") {              
              let roomsetting = res.data.data   // 可能为null
              this.title = (roomsetting && roomsetting.shareTitel) || '【' + this.$store.state.SysOrgInfo.name + '直播】让思想的碰撞改变投资，专业，好看！', // 分享标题
              this.message = (roomsetting && roomsetting.shareSubtitle) || "现在下载" + this.$store.state.SysOrgInfo.name + "APP，速领银豆惊喜大礼。", //分享描述
              this.imgUrl = (roomsetting && roomsetting.shareImgurl) || this.shareImgUrl    
              let shareUrl = "liveRoom/" + this.roomId + "?fromUn=" + this.$store.state.userInfo.un;
			      	this.urlLink = fun.getShareUrl(this.$channel,shareUrl);
              this.sharefunction(this.urlLink, this.title, this.message, this.imgUrl)
              // window.MyWebView.tzt_hsCallNativeFunctionFromWeb("reqaction", "{\"action\":\"10055\",\"url\":\"" + this.urlLink + "\",\"title\":\"" + this.title + "\",\"message\":\"" + this.message + "\",\"IMG_URL\":\""+ this.imgUrl +"\"}") // 方案三                
            }
          }).catch(err => {
            console.log(err)
            reject(err)
          });
        })
      },
      // 获取回看的分享配置信息
      getLookBackInfo(){
        return new Promise((resolve, reject)=>{
          this.$http.get("/h5live/getBackLiveInfo?vid=" + this.lookBackId + "&plat=" + fun.getPlatform()).then(res => {            
            if(res.data.code === "000000") {              
              let lookBack_xq = res.data.data.videoInfo   // 可能为null
              this.title = (lookBack_xq && lookBack_xq.shareTitle) || '【' + this.$store.state.SysOrgInfo.name + '直播】让思想的碰撞改变投资，专业，好看！', // 分享标题
              this.message = (lookBack_xq && lookBack_xq.shareSubtitle) || "现在下载" + this.$store.state.SysOrgInfo.name + "APP，速领银豆惊喜大礼。", //分享描述
              this.imgUrl = (lookBack_xq && lookBack_xq.shareImgurl) || this.shareImgUrl
              let shareUrl =  "liveBack/" + this.lookBackId + "?fromUn=" + this.$store.state.userInfo.un;
		        	this.urlLink = fun.getShareUrl(this.$channel,shareUrl);
              this.sharefunction(this.urlLink, this.title, this.message, this.imgUrl)
              // window.MyWebView.tzt_hsCallNativeFunctionFromWeb("reqaction", "{\"action\":\"10055\",\"url\":\"" + this.urlLink + "\",\"title\":\"" + this.title + "\",\"message\":\"" + this.message + "\",\"IMG_URL\":\""+ this.imgUrl +"\"}") // 方案三                 
            }
          }).catch(err => {
            console.log(err)
            reject(err)
          });
        })
      },      
      // async 
      shareBtnHandle(){   
        // window.MyWebView.tzt_hsCallNativeFunctionFromWeb("reqaction", "{\"action\":\"10055\",\"url\":\"www.baidu.com\",\"title\":\"标题\",\"message\":\"内容\",\"IMG_URL\":\"图片地址\"}")                  
        // 获取标题、内容
        switch(this.type){
          case '0': return this.getHomeInfo(); break;
          case '1': return this.getLiveInfo(); break;          
          case '2': return this.getLookBackInfo(); break;          
        }         
      }
    }
  }
</script>

<style scoped>
.share-btn-wrap{
  position: fixed;
  /* top: 70vh;
  right:  10vw; */  
}
.share-btn-image{  
  width: 70px;
  height: 70px;
  background-image: url('../../assets/images/zyzq/share.png');
  background-size: 100% 100%;
}
</style>